package com.xingkeduo.service.cms.marketing.impl;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xingkeduo.dao.TMarketingCardMapper;
import com.xingkeduo.dao.TMarketingCouponBagMapper;
import com.xingkeduo.dao.TMarketingCouponMapper;
import com.xingkeduo.dao.TMarketingMapperSpec;
import com.xingkeduo.dao.TMarketingTranInfoCardMapper;
import com.xingkeduo.dao.TMarketingTranInfoCouponBagMapper;
import com.xingkeduo.dao.TMarketingTranInfoCouponMapper;
import com.xingkeduo.model.param.MarketingParam.MarketingTypeEnum;
import com.xingkeduo.model.po.TMarketing;
import com.xingkeduo.model.po.TMarketingTranInfo;
import com.xingkeduo.model.po.TMarketingTranInfoCard;
import com.xingkeduo.model.po.TMarketingTranInfoCoupon;
import com.xingkeduo.model.po.TMarketingTranInfoCouponBag;
import com.xingkeduo.service.cms.marketing.InventoryService;
import com.xingkeduo.util.tool.ResponseMessage;

@Service
public class InventoryServiceImpl implements InventoryService {
	Logger logger = LoggerFactory.getLogger(InventoryServiceImpl.class);
	@Autowired
	TMarketingMapperSpec tMarketingMapperSpec;
	@Autowired
	TMarketingCouponMapper tMarketingCouponMapper;
	@Autowired
	TMarketingCardMapper tMarketingCardMapper;
	@Autowired
	TMarketingCouponBagMapper tMarketingCouponBagMapper;
	@Autowired
	TMarketingTranInfoCouponMapper tMarketingTranInfoCouponMapper;
	@Autowired
	TMarketingTranInfoCardMapper tMarketingTranInfoCardMapper;
	@Autowired
	TMarketingTranInfoCouponBagMapper tMarketingTranInfoCouponBagMapper;
	

	@Override
	public ResponseMessage addInventoryModificationRecord(Long martktingId, Integer quantity,Long createBy,Integer marketingType) {
		if (martktingId == null) {
			return ResponseMessage.error("券包id不合法", 500);
		}
		if (quantity == null || quantity.intValue() <= 0) {
			return ResponseMessage.error("券库存数量不合法", 500);
		}
		TMarketingTranInfo tMarketingTranInfo = new TMarketingTranInfo(martktingId, martktingId,
				0, quantity, quantity,
				"初始化"+MarketingTypeEnum.parseStatus(marketingType).getDesc()+"，新增库存", createBy.toString(), new Date());
		if(marketingType.equals(MarketingTypeEnum.COUPON.getCode())) {
			TMarketingTranInfoCoupon tranInfoCoupon = new TMarketingTranInfoCoupon();
			BeanUtils.copyProperties(tMarketingTranInfo, tranInfoCoupon);
			tMarketingTranInfoCouponMapper.insertSelective(tranInfoCoupon);
		}else if(marketingType.equals(MarketingTypeEnum.CARD.getCode())) {
			TMarketingTranInfoCard tranInfoCard = new TMarketingTranInfoCard();
			BeanUtils.copyProperties(tMarketingTranInfo, tranInfoCard);
			tMarketingTranInfoCardMapper.insertSelective(tranInfoCard);
		}else if(marketingType.equals(MarketingTypeEnum.COUPON_BAG.getCode())) {
			TMarketingTranInfoCouponBag tranInfoCouponBag = new TMarketingTranInfoCouponBag();
			BeanUtils.copyProperties(tMarketingTranInfo, tranInfoCouponBag);
			tMarketingTranInfoCouponBagMapper.insertSelective(tranInfoCouponBag);
		}
		return ResponseMessage.ok("新增库存成功");
	}

	@Override
	public ResponseMessage addInventoryModificationRecord(Long martktingId, Long bizId, Integer subQuantity,
			String memo,Long createBy,Integer marketingType) {
		logger.info("addInventoryModificationRecord_param:{},:{},:{}",martktingId,bizId,subQuantity);
		if (martktingId == null) {
			return ResponseMessage.error("券包id不合法", 500);
		}
		if (subQuantity == null || subQuantity.intValue() <= 0) {
			return ResponseMessage.error("券库存数量不合法", 500);
		}
		TMarketing maketing = selectByPrimaryKey(martktingId,marketingType);
		if (maketing == null) {
			return ResponseMessage.error("券包信息为空", 500);
		}
		int count = tMarketingMapperSpec.updateMarketingInventorySubQuantity(martktingId,marketingType, subQuantity);
		if (count == 0) {
			return ResponseMessage.error("库存更新失败", 500);
		}
		TMarketingTranInfo tMarketingTranInfo = new TMarketingTranInfo(martktingId, bizId,
				subQuantity.intValue() < 0 ? 0 : (subQuantity.intValue() == 0 ? 2 : 1),
				maketing.getSurplusQuantity().intValue() - subQuantity, subQuantity, memo, createBy.toString(), new Date());
		if(marketingType.equals(MarketingTypeEnum.COUPON.getCode())) {
			TMarketingTranInfoCoupon tranInfoCoupon = new TMarketingTranInfoCoupon();
			BeanUtils.copyProperties(tMarketingTranInfo, tranInfoCoupon);
			tMarketingTranInfoCouponMapper.insertSelective(tranInfoCoupon);
		}else if(marketingType.equals(MarketingTypeEnum.CARD.getCode())) {
			TMarketingTranInfoCard tranInfoCard = new TMarketingTranInfoCard();
			BeanUtils.copyProperties(tMarketingTranInfo, tranInfoCard);
			tMarketingTranInfoCardMapper.insertSelective(tranInfoCard);
		}else if(marketingType.equals(MarketingTypeEnum.COUPON_BAG.getCode())) {
			TMarketingTranInfoCouponBag tranInfoCouponBag = new TMarketingTranInfoCouponBag();
			BeanUtils.copyProperties(tMarketingTranInfo, tranInfoCouponBag);
			tMarketingTranInfoCouponBagMapper.insertSelective(tranInfoCouponBag);
		}
		return ResponseMessage.ok("更新库存成功");
	}
	
	
	private TMarketing selectByPrimaryKey(Long supportBusinessId, Integer marketingType) {
		if(marketingType.equals(MarketingTypeEnum.COUPON.getCode())) {
			return tMarketingCouponMapper.selectByPrimaryKey(supportBusinessId);
		}else if(marketingType.equals(MarketingTypeEnum.CARD.getCode())) {
			return tMarketingCardMapper.selectByPrimaryKey(supportBusinessId);
		}else if(marketingType.equals(MarketingTypeEnum.COUPON_BAG.getCode())) {
			return tMarketingCouponBagMapper.selectByPrimaryKey(supportBusinessId);
		}
		return null;
	}

}
